﻿/// <reference path="../../Scripts/jquery-1.4.1-vsdoc.js" />
/// <reference path="../../Content/Scripts/Common.js" />

//这个属性由页面动态设置数据
var error = '';
var initRows = true;

var bigCates = null;

$(function () {
    bigCates = im_materials; //这个变量在母版页中定义。
});

function bindSeries(selectNo) {
    //以下两个方法用到了selectNo
    var rebindMaterials = function () {
        var subCate = $("#CategoryId_" + selectNo);
        var id = $selInt(subCate[0]);
        for (var i = 0; i < bigCates.length; i++) {
            var bigCate = bigCates[i];
            for (var j = 0; j < bigCate.subCates.length; j++) {
                var subCate = bigCate.subCates[j];
                if (subCate.value == id) {
                    var e = $("#MaterialId_" + selectNo);
                    $bindSelect(e[0], subCate.materials);
                    if (selectedMaterialId > 0) $selectValue(e[0], selectedMaterialId);
                }
            }
        }
    }
    var rebindSubCates = function () {
        var bigCate = $("#BigCategoryId_" + selectNo);
        var id = $selInt(bigCate[0]);
        for (var i = 0; i < bigCates.length; i++) {
            var bigCate = bigCates[i];
            if (bigCate.value == id) {
                var e = $("#CategoryId_" + selectNo);
                $bindSelect(e[0], bigCate.subCates);
                if (selectedSubCateId > 0) $selectValue(e[0], selectedSubCateId);

                rebindMaterials();
                e.change(rebindMaterials);
            }
        }
    }

    var selectedMaterialId = parseInt($("#SelectedMaterialId_" + selectNo).val());
    var m = im_findMaterial(selectedMaterialId);
    var selectedBigCateId = null;
    var selectedSubCateId = null;
    if (m != null) {
        selectedBigCateId = m.bigCateId;
        selectedSubCateId = m.pid;
    }

    var e = $("#BigCategoryId_" + selectNo);
    $bindSelect(e[0], bigCates);
    if (selectedBigCateId > 0) $selectValue(e[0], selectedBigCateId);

    rebindSubCates();
    e.change(rebindSubCates);
}

//应用程序启动时绑定所有数据
$(function () {
    var no = parseInt($("#MaterialNumbers").val());
    for (var i = 1; i <= no; i++) {
        bindSeries(i);
    }

    altTable($("#materialTable"));

    if (error.length > 0) alert(error);
});

function delMaterial(no) {
    var rows = $("#materialContainer tr").length;
    if (rows == 1) {
        alert("最后一行，不能删除。");
    }
    else {
        $("#tr_" + no).remove();
    }
}

function addMaterial() {
    var v = $("#MaterialNumbers").val();
    v = parseInt(v) + 1;
    $("#MaterialNumbers").val(v);

    var htmlSegment = '\
<tr id="tr_<%=i %>">\
	<td style="text-align: center;">\
		<input type="hidden" id="SelectedMaterialId_<%=i %>" />\
		<select id="BigCategoryId_<%=i %>" ></select>\
		<select id="CategoryId_<%=i %>" ></select>\
		<select name="MaterialId_<%=i %>" id="MaterialId_<%=i %>" ></select>\
	</td>\
	<td>\
		<input type="text" name="Numbers_<%=i %>" value="0"/>\
	</td>\
	<td>\
		<input type="text" name="DeliveryTime_<%=i %>" onclick="setday(this);" />\
	</td>\
	<td>\
		<input type="text" name="PayMode_<%=i %>" />\
	</td>\
	<td>\
	<div>\
		<a href="javascript:;" onclick="delMaterial(<%=i %>);">\
			<img style="width:16px; height:16px;" src="' + mapUrl("~/images/c/delete.png") + '" border="0" alt="删除" /></a>\
	</div>\
	</td>\
</tr>\
';
    htmlSegment = htmlSegment.replace(/<%=i %>/g, v);

    var c = $("#materialContainer");
    c.append(htmlSegment);

    bindSeries(v);

    altTable($("#materialTable"));
}

var rowCount = 0;
function AddRows() {
    if (!initRows) return;

    addMaterial();
    rowCount++;
    if (rowCount < 5) {
        setTimeout(AddRows, 100);
    }
}
$(AddRows);

function showMessage() {
    alert("提交成功!等待审核");
}